﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using public_dbOpt;

namespace prjEMR22_154
{
    public partial class Pa_Md : Form
    {
        dbOPT dataOpt = new dbOPT();
        public static string strMedicineID;

        public Pa_Md()
        {
            InitializeComponent();
        }

        private void Pa_Md_Load(object sender, EventArgs e)
        {
            string strHspID = PatientInfo_inquire_checked.strHspID;
            string sql = String.Format("select * from patient_medicine where HspID = '{0}';", strHspID);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                // 访问数据集中的数据  
                DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                object value = row["医嘱"];
                object value_1 = row["MedicineID"];
                if (value != DBNull.Value) // 检查值是否为 DBNull  
                {
                    医嘱.Text = value.ToString();
                    strMedicineID = value_1.ToString();
                }
                sql = String.Format("select * from medicineinfo where MedicineID = '{0}';", strMedicineID);
                ds = dataOpt.GetTable(sql);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                    InitBoxes(this, row);//初始化设置各个控件的初始值
                }
            }
            else
            {
                MessageBox.Show("用药为空。");
            }
            foreach (Control ctrl in this.Controls)   //关闭药品信息可编辑
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    string columnName = ctrl.Name;

                    if (columnName != "医嘱")
                    {
                        textBox.ReadOnly = true;
                    }
                    // 如果列不存在，可以选择记录日志、抛出异常或执行其他操作  
                    // else { /* 处理列不存在的情况 */ }  
                }

            }
        }

        private void InitBoxes(Control parent, DataRow row)  //初始化设置各个控件的初始值
        {
            foreach (Control ctrl in parent.Controls)
            {
                if (ctrl is TextBox)
                {
                    TextBox textBox = (TextBox)ctrl;
                    string columnName = ctrl.Name;

                    // 检查列是否存在并尝试获取值  
                    if (row.Table.Columns.Contains(columnName))
                    {
                        object value = row[columnName];
                        if (value != DBNull.Value) // 检查值是否为 DBNull  
                        {
                            textBox.Text = value.ToString();
                        }
                    }
                    // 如果列不存在，可以选择记录日志、抛出异常或执行其他操作  
                    // else { /* 处理列不存在的情况 */ }  
                }

            }

        }




        private void Save_Click(object sender, EventArgs e)
        {
            string strHspID = PatientInfo_inquire_checked.strHspID;
            string sql = String.Format("select * from patient_medicine where HspID = '{0}' and MedicineID = '{1}';", strHspID,MedicineID.Text);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                sql = String.Format("UPDATE patient_medicine SET MedicineID = '{0}' ,医嘱 = '{1}' where HspID = '{2}'", MedicineID.Text, 医嘱.Text, strHspID);   //set后跟的属性不能加''
                dataOpt.OperateData(sql);
            }
            else
            {
                sql = String.Format("INSERT INTO patient_medicine (HspID) VALUES ({0})", strHspID);
                dataOpt.OperateData(sql);
                sql = String.Format("UPDATE patient_medicine SET MedicineID = '{0}' ,医嘱 = '{1}' where HspID = '{2}'", MedicineID.Text, 医嘱.Text, strHspID);   //set后跟的属性不能加''
                dataOpt.OperateData(sql);
                
            }
            MessageBox.Show("保存成功");
        }

        private void Change_Click(object sender, EventArgs e)
        {
            Pa_Md_search a = new Pa_Md_search();
            a.ShowDialog();
            if (Pa_Md_search.sure_check)
            {
                strMedicineID = Pa_Md_search.strMedicineID;

                string sql = String.Format("select * from medicineinfo where MedicineID = '{0}';", strMedicineID);
                DataSet ds = dataOpt.GetTable(sql);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                    InitBoxes(this, row);//初始化设置各个控件的初始值
                }
            }
        }

        private void Cencel_Click(object sender, EventArgs e)
        {
            string strHspID = PatientInfo_inquire_checked.strHspID;
            string sql = String.Format("select * from patient_medicine where HspID = '{0}';", strHspID);
            DataSet ds = new DataSet();//创建数据集
            ds = dataOpt.GetTable(sql);

            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                // 访问数据集中的数据  
                DataRow row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                object value = row["医嘱"];
                object value_1 = row["MedicineID"];
                if (value != DBNull.Value) // 检查值是否为 DBNull  
                {
                    医嘱.Text = value.ToString();
                    strMedicineID = value_1.ToString();
                }
                sql = String.Format("select * from medicineinfo where MedicineID = '{0}';", strMedicineID);
                ds = dataOpt.GetTable(sql);
                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    row = ds.Tables[0].Rows[0]; // 获取第一行数据  
                    InitBoxes(this, row);//初始化设置各个控件的初始值
                } 
            }
        }
    }
}
